from datetime import datetime

from django.db import models


# Create your models here.
class Userinfo(models.Model):
    phone = models.CharField(max_length=16, verbose_name="手机号")
    password = models.CharField(max_length=255, verbose_name="密码")
    name = models.CharField(max_length=16, verbose_name="姓名", null=True, blank=True)
    token = models.CharField(max_length=255, verbose_name="token", null=True, blank=True)
    seax = (
        ('1', '男'),
        ('2', '女')
    )
    sex = models.CharField(max_length=16, verbose_name="性别", choices=seax, null=True, blank=True)
    age = models.IntegerField(verbose_name="年龄", null=True, blank=True)
    portrait = models.ImageField(upload_to="avitar/", max_length=128, verbose_name="头像", null=True, blank=True)
    code = models.CharField(max_length=16, verbose_name="验证码", null=True, blank=True)
    address = models.CharField(max_length=64, verbose_name="住址", null=True, blank=True)
    balance = models.DecimalField(verbose_name='余额', decimal_places=2, max_digits=8)
    flag = models.BooleanField(verbose_name='状态')
    create_time = models.DateTimeField(verbose_name='创建时间')
    update_time = models.DateTimeField(verbose_name='更新时间')
    vip_status = models.IntegerField(default=0, verbose_name='优惠vip状态')

    class Meta:
        db_table = 'Userinfo'
        verbose_name = "用户管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.phone


class Plate(models.Model):
    users = models.ForeignKey("Userinfo", on_delete=models.CASCADE, verbose_name="关联用户")
    plate_number = models.CharField(max_length=16, verbose_name="车牌号")

    class Meta:
        db_table = 'Plate'
        verbose_name = "车牌管理"
        verbose_name_plural = verbose_name


class use_records(models.Model):
    user = models.ForeignKey("Userinfo", on_delete=models.CASCADE, verbose_name="用户")
    plate_number = models.CharField(max_length=16, verbose_name="车牌号")
    sexas = (
        ('chu', '出库'),
        ('ru', '入库'),
    )
    status = models.CharField(choices=sexas, max_length=16, verbose_name="状态")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="记录时间")
    park_id = models.IntegerField(verbose_name='停车场id')

    class Meta:
        ordering = ['-add_time']
        db_table = 'use_records'
        verbose_name = "出入库记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.plate_number


class no_register_records(models.Model):
    plate_number = models.CharField(max_length=16, verbose_name="车牌号")
    sexas = (
        ('chu', '出库'),
        ('ru', '入库'),
    )
    status = models.CharField(choices=sexas, max_length=16, verbose_name="状态")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="记录时间")
    park_id = models.IntegerField(verbose_name='停车场id')

    class Meta:
        ordering = ['-add_time']
        db_table = 'no_register_records'
        verbose_name = "未注册车辆出入库记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.plate_number


class all_records(models.Model):
    plate_number = models.CharField(max_length=16, verbose_name="车牌号")

    in_time = models.DateTimeField(default=datetime.now, verbose_name="进入时间")
    out_time = models.DateTimeField(default=datetime.now, verbose_name="出去时间")

    pay = models.FloatField(default=0, verbose_name='费用')
    park_id = models.IntegerField(verbose_name='停车场id')

    is_register_flag = models.BooleanField(default=1, verbose_name="注册标志")

    class Meta:
        db_table = 'all_records'
        verbose_name = "出入库总记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.plate_number) + str(self.park_id)


class park_info(models.Model):
    park_id = models.IntegerField(verbose_name='停车场id')
    park_name = models.CharField(verbose_name='停车场名', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间')
    update_time = models.DateTimeField(verbose_name='更新时间')
    place = models.CharField(verbose_name='位置', max_length=255)
    capacity = models.IntegerField(verbose_name='容量')

    class Meta:
        db_table = 'park_info'
        verbose_name = '停车场信息'
        verbose_name_plural = verbose_name


class parking_lot(models.Model):
    lot_id = models.IntegerField(verbose_name='车位id')
    flag = models.BooleanField(verbose_name='状态', default=True)
    part = models.CharField(verbose_name='区域', max_length=11)
    create_time = models.DateTimeField(verbose_name='创建时间')
    update_time = models.DateTimeField(verbose_name='更新时间')
    park_id = models.IntegerField(verbose_name='停车场id')

    class Meta:
        db_table = 'parking_lot'
        verbose_name = '车位信息'
        verbose_name_plural = verbose_name


class Info(models.Model):
    name = models.ForeignKey("Userinfo", on_delete=models.CASCADE, verbose_name="用户")
    text = models.TextField(verbose_name="反馈的问题")
    reply = models.TextField(null=True, blank=True, verbose_name="回复")

    class Meta:
        verbose_name = "反馈管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.phone


class Spaces(models.Model):
    num = models.IntegerField(verbose_name="车位总数量", default=50)
    shengyu = models.IntegerField(verbose_name="剩余车位数量", default=50)
    park_id = models.IntegerField(verbose_name='停车场id')

    class Meta:
        verbose_name = "车位管理"
        verbose_name_plural = verbose_name

    def __int__(self):
        return self.num

